package cn.com.controller;

import cn.com.beans.AdmissionInfo;
import cn.com.beans.CollegeInfo;
import cn.com.beans.MajorInfo;
import cn.com.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@Controller
@RequestMapping("/college")
public class CollegeController {
    @Resource
    private CollegeInfoService collegeInfoService;
    @Resource
    private AdmissionInfoService admissionInfoService;
    @Resource
    private MajorInfoService majorInfoService;
    @Resource
    private MajorLineService majorLineService;
    @Resource
    private SalaryInfoService salaryInfoService;


    @Resource
    private FractionInfoService fractionInfoService;


    @GetMapping("/collegelist")
    public String College(ModelMap modelMap,Integer page,CollegeInfo collegeInfo,String prid,String category,String level,String features,String type) {
        if(prid != null){
            collegeInfo.setCoPrid(prid);
        }
        if(category != null){
            collegeInfo.setCoCategory(category);
        }
        if(level != null){
            collegeInfo.setCoLevel(level);
        }
        if(features != null){
            collegeInfo.setCoFeatures(features);
        }
        if(type != null){
            collegeInfo.setCoType(type);
        }
        if(page == null){
            page = 1;
        }
        List<CollegeInfo> collegeInfoList = collegeInfoService.findAll(collegeInfo);
        Integer sum = this.collegeInfoService.findRownum(collegeInfo);
        modelMap.addAttribute("college", collegeInfo);
        modelMap.addAttribute("collegeList", collegeInfoList);
        modelMap.addAttribute("cpage",page);
        modelMap.addAttribute("sum",sum);
        int lastPage = sum/6+1;
        modelMap.addAttribute("lastpage", lastPage);
        return "query/collegeList";
    }

    @GetMapping("/find")
    public String find(ModelMap modelMap,Integer id) {
        List<MajorInfo> majorList = this.majorInfoService.findByCoid(id);
        CollegeInfo collegeInfo = this.collegeInfoService.selectByPrimaryKey(id);
        Integer year1 = fractionInfoService.findScoreByYearAndId(2015,id,0);
        Integer year2 = fractionInfoService.findScoreByYearAndId(2016,id,0);
        Integer year3 = fractionInfoService.findScoreByYearAndId(2017,id,0);
        Integer year4 = fractionInfoService.findScoreByYearAndId(2018,id,0);
        Integer year5 = fractionInfoService.findScoreByYearAndId(2019,id,0);
        modelMap.addAttribute("year1",year1);
        modelMap.addAttribute("year2",year2);
        modelMap.addAttribute("year3",year3);
        modelMap.addAttribute("year4",year4);
        modelMap.addAttribute("year5",year5);
        modelMap.addAttribute("college", collegeInfo);
        modelMap.addAttribute("majorlist", majorList);
        return "query/collegeInfo";
    }
    @GetMapping("/findtype")
    public String find(ModelMap modelMap,Integer id,String type) {
        CollegeInfo collegeInfo = this.collegeInfoService.selectByPrimaryKey(id);
        modelMap.addAttribute("college", collegeInfo);
        if(type.equals("1")) {
            int i = 1;
            int j = 0;
            List<MajorInfo> majorList = this.majorInfoService.findByCoid(id);
            for(MajorInfo major:majorList) {
                Integer mid = major.getMaId();
                String name = this.majorInfoService.findNameById(mid);
                modelMap.addAttribute("name"+i, name);
                List<Integer> score = this.majorLineService.findScore(id, mid);
                Integer[] scores = new Integer[score.size()];
                score.toArray(scores);
                for (Integer s:scores) {
                    j++;
                    modelMap.addAttribute("score"+j,s);
                }
                i++;
            }
            modelMap.addAttribute("number",j);
            return "query/majorLine";
        }else if(type.equals("2")) {
            AdmissionInfo admissionInfo = this.admissionInfoService.findByCoid(id);
            modelMap.addAttribute("admission",admissionInfo);
            return "query/constitution";
        }
        else{
            for(int i = 1;i <= 11;i++) {
                int money = this.salaryInfoService.findMoneyByCoidAndYear(id, i);
                modelMap.addAttribute("money"+i,money);
            }
            return "query/salary";
        }
    }
}
